package la.bbs558.controller.admin;

import cn.dev33.satoken.annotation.SaCheckRole;
import la.bbs558.aop.valid.Check;
import la.bbs558.aop.valid.Valid;
import la.bbs558.controller.base.Result;
import la.bbs558.entity.User;
import la.bbs558.entity.params.UserSearchPage;
import la.bbs558.entity.params.UserSignUpParams;
import la.bbs558.satoken.StpAdminUtil;
import la.bbs558.service.UserService;
import la.bbs558.util.ExcelUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author: Shanks
 * @date: 2023/12/15 15:30
 * @desc:
 */
@RestController
@RequestMapping("/admin/user")
@RequiredArgsConstructor
@SaCheckRole(value = { "admin" },type = StpAdminUtil.TYPE)
@Check
public class UserManageController {

    private final UserService userService;

    @GetMapping("list")
    public Result<?> list(@Valid UserSearchPage searchPage) {
        userService.listUserPageForAdmin(searchPage);
        return Result.success(searchPage);
    }

    @PostMapping("updateState")
    public Result<?> updateState(@RequestBody User user) {
        userService.updateState(user);
        return Result.success();
    }

    @PostMapping("resetPassword")
    public Result<?> resetPassword(@RequestBody User user) {
        User updateUser = new User()
                .setId(user.getId())
                .setPassword(user.getPassword());
        userService.updateById(updateUser);
        return Result.success();
    }

    /**
     * 解析excel批量注册
     * @param file
     * @return
     */
    @PostMapping("parseExcel2Register")
    public Result<?> parseExcel2Register(MultipartFile file) {
        List<UserSignUpParams> userSignUpParams = ExcelUtil.parseExcel2Register(file);
        userService.batchRegister(userSignUpParams);
        return Result.success(userSignUpParams);
    }

    // @PostMapping("batchRegister")
    // public Result<?> batchRegister(@RequestBody List<UserSignUpParams> userSignUpParams) {
    //     for (UserSignUpParams user : userSignUpParams) {
    //         user.setPassword(SecureUtil.md5(user.getPassword()));
    //     }
    //     userService.batchRegister(userSignUpParams);
    //     return Result.success();
    // }
}
